package csu.web.mypetstore.service;

import csu.web.mypetstore.domain.Item;
import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.persistence.ItemDao;
import csu.web.mypetstore.persistence.OrderDao;
import csu.web.mypetstore.persistence.impl.ItemDaoImpl;
import csu.web.mypetstore.persistence.impl.OrderDaoImpl;

import java.util.List;

public class OrderService {

    private ItemDao itemDAO;
    private OrderDao orderDAO;
/*  private SequenceDAO sequenceDAO;
  private LineItemDAO lineItemDAO;*/

    public OrderService() {
        /*sequenceDAO = new SequenceDAOImpl();*/
        orderDAO = new OrderDaoImpl();
        itemDAO = new ItemDaoImpl();
        /*lineItemDAO = new LineItemDAOImpl();*/
    }

    private Item itemMapper;

    private Order orderMapper;

/*  private Sequence sequenceMapper;

  private LineItem lineItemMapper;*/


    public void insertOrder(Order order) {
    /*for (int i = 0; i < order.getLineItems().size(); i++) {
      LineItem lineItem = (LineItem) order.getLineItems().get(i);
      String itemId = lineItem.getItemId();
      Integer increment = new Integer(lineItem.getQuantity());
      Map<String, Object> param = new HashMap<String, Object>(2);
      param.put("itemId", itemId);
      param.put("increment", increment);
      itemDAO.updateInventoryQuantity(param);
    }*/

        orderDAO.insertOrder(order);
        /*orderDAO.insertOrderStatus(order);*/
    /*for (int i = 0; i < order.getLineItems().size(); i++) {
      LineItem lineItem = (LineItem) order.getLineItems().get(i);
      lineItem.setOrderId(order.getOrderId());
      lineItemDAO.insertLineItem(lineItem);
    }*/
    }

/*  public Order getOrder(int orderId) {
    Order order = orderDAO.getOrder(orderId);
    order.setLineItems(lineItemDAO.getLineItemsByOrderId(orderId));

    for (int i = 0; i < order.getLineItems().size(); i++) {
      LineItem lineItem = (LineItem) order.getLineItems().get(i);
      Item item = itemDAO.getItem(lineItem.getItemId());
      item.setQuantity(itemDAO.getInventoryQuantity(lineItem.getItemId()));
      lineItem.setItem(item);
    }

    return order;
  }*/

    public List<Order> getOrdersByUsername(String username) {
        return orderDAO.getOrdersByUsername(username);
    }

    public Order getOrderById(int orderId){
        return orderDAO.getOrder(orderId);
    }

/*  public int getNextId(String name) {
    Sequence sequence = new Sequence(name, -1);
    sequence = (Sequence) sequenceDAO.getSequence(sequence);
    if (sequence == null) {
      throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name
          + " sequence).");
    }
    Sequence parameterObject = new Sequence(name, sequence.getNextId() + 1);
    sequenceDAO.updateSequence(parameterObject);
    return sequence.getNextId();
  }*/
}
